var applicationName = "FinalProject";

function reload(timeout){
	setTimeout(function(){
		window.location.reload();
	},timeout);
}

function trim(str){return str.replace(/^\s+|\s+$/g,"");}

function validateLogin() {
	
	document.getElementById("error").innerHTML = "";
	
	var username = document.getElementById("username").value;
	var password = document.getElementById("password").value;
	
	if (!validateUserPassword(username, password)) {
		document.getElementById("error").innerHTML = "Todos os campos devem ser preenchidos";
		return false;
	}
	
	url = "validate?type=LOGIN&username=" + username + "&password=" + password;
	
	jQuery.get(url,function(data){validateResult(data, "menu");});
}

function validateUserPassword(username, password) {
	if (username == '') {
		return false;
	}
	
	if (password == '') {
		return false;
	}
	
	return true;
}

function validateResult(data, location) {
	data = trim(data);
	
	if (data == "SUCCESS") {
		window.location = location;
	} else if (data == "NOT_USER") {
		document.getElementById("error").innerHTML = "Usuario nao cadastrado";
	} else if (data == "INVALID_PASSWORD") {
		document.getElementById("error").innerHTML = "Senha invalida";
	} else if (data == "ERROR") {
		document.getElementById("error").innerHTML = "Erro ao tentar logar, tente novamente mais tarde";
	} 
}

function doLogout () {
	url = "validate?type=LOGOUT";
	jQuery.get(url,function(data){validateResult(data, "admin");});
}